package com.hyx.api.service.impl.pc_admin;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.hyx.api.domain.RecycleBusiness;
import com.hyx.api.domain.bo.RecycleBusinessBo;
import com.hyx.api.domain.vo.RecycleBusinessVo;
import com.hyx.api.mapper.RecycleBusinessMapper;
import com.hyx.api.service.pc_admin.IRecycleBusinessService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;

/**
 * 回收商信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-20
 */
@RequiredArgsConstructor
@Service
public class RecycleBusinessServiceImpl implements IRecycleBusinessService {
    private final RecycleBusinessMapper recycleBusinessMapper;

    /**
     * 查询回收商信息
     *
     * @param id 回收商信息主键
     * @return 回收商信息
     */
    @Override
    public RecycleBusinessVo selectRecycleBusinessById(Long id) {
        //TODO 表以外的字段，写入vo
        return recycleBusinessMapper.selectVoById(id);
    }

    /**
     * 查询回收商信息列表
     *
     * @param bo 回收商信息
     * @return 回收商信息
     */
    @Override
    public List<RecycleBusinessVo> selectRecycleBusinessList(RecycleBusinessBo bo) {
        LambdaQueryWrapper<RecycleBusiness> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtils.isNotEmpty(bo.getId()), RecycleBusiness::getId, bo.getId());
        lqw.like(ObjectUtils.isNotEmpty(bo.getName()), RecycleBusiness::getName, bo.getName());
        lqw.like(ObjectUtils.isNotEmpty(bo.getAddress()), RecycleBusiness::getAddress, bo.getAddress());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getStatus()), RecycleBusiness::getStatus, bo.getStatus());
        return recycleBusinessMapper.selectVoList(lqw);
    }

    /**
     * 新增回收商信息
     *
     * @param bo 回收商信息
     * @return 结果
     */
    @Override
    public int insertRecycleBusiness(RecycleBusinessBo bo) {
        RecycleBusiness recycleBusiness = tranEntityBeforeSave(bo);
        recycleBusiness.setCreateBy(SecurityUtils.getUsername());
        recycleBusiness.setCreateTime(DateUtils.getNowDate());
        validEntityBeforeSave(recycleBusiness);
        return recycleBusinessMapper.insert(recycleBusiness);
    }

    /**
     * 修改回收商信息
     *
     * @param bo 回收商信息
     * @return 结果
     */
    @Override
    public int updateRecycleBusiness(RecycleBusinessBo bo) {
        RecycleBusiness recycleBusiness = tranEntityBeforeSave(bo);
        recycleBusiness.setUpdateBy(SecurityUtils.getUsername());
        recycleBusiness.setUpdateTime(DateUtils.getNowDate());
        return recycleBusinessMapper.updateById(recycleBusiness);
    }

    /**
     * 批量删除回收商信息
     *
     * @param ids 需要删除的回收商信息主键
     * @return 结果
     */
    @Override
    public int deleteRecycleBusinessByIds(Long[] ids) {
        return recycleBusinessMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RecycleBusiness entity) {
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<RecycleBusiness> lqw = Wrappers.lambdaQuery();
        lqw.eq(RecycleBusiness::getName, entity.getName());
        Long count = recycleBusinessMapper.selectCount(lqw);
        if (count > 0) {
            throw new ServiceException("回收商已存在");
        }
    }

    /**
     * 保存前的数据转换
     */
    private RecycleBusiness tranEntityBeforeSave(RecycleBusinessBo bo) {
        //TODO 做一些数据转换
        RecycleBusiness recycleBusiness = new RecycleBusiness();
        BeanUtils.copyProperties(bo, recycleBusiness);
        return recycleBusiness;
    }
}
